package com.dh.project.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.dh.project.model.*;
import com.dh.project.model.common.Statistics;
import com.dh.project.other.ModelRes;
import com.dh.project.service.*;
import com.dh.project.utils.CodeUtil;
import com.dh.project.utils.DayuSMSUtil;
import com.dh.project.utils.StringUtil;
import com.dh.project.view.CouponView;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.*;

/**
 * 客户操作Controller
 * Created by admin on 2017/3/29.
 */
@Controller
public class CustomerController implements Runnable {

    private CustomerInfoService customerInfoService;

    private CustomerAddressService customerAddressService;

    private CustomerCouponService customerCouponService;

    private CustomerIdeaService customerIdeaService;

    private CustomerInsuranceService customerInsuranceService;
    @Autowired
    CustomerController t;

    @Autowired
    public CustomerController(CustomerInfoService customerInfoService, CustomerAddressService customerAddressService,
                              CustomerCouponService customerCouponService, CustomerIdeaService customerIdeaService,
                              CustomerInsuranceService customerInsuranceService, CouponService couponService) {
        this.customerInfoService = customerInfoService;
        this.customerAddressService = customerAddressService;
        this.customerCouponService = customerCouponService;
        this.customerIdeaService = customerIdeaService;
        this.customerInsuranceService = customerInsuranceService;
        this.couponService = couponService;

    }

    Statistics statistics = new Statistics();

    /**
     * 发送验证码
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "sendSms", produces = "text/html;charset=utf-8")
    public Object sendSms(String customer_phone, int type) {
        Map map = new HashMap();
        map.put("customer_phone", customer_phone);
        List<CustomerInfo> customerInfoList = customerInfoService.selectByMap(map);
        if (type == 1) {
            if (customerInfoList.size() == 0) {
                return new ModelRes(ModelRes.Status.ERROR, "没有该用户！");
            } else {
                String mathCode = String.valueOf(CodeUtil.getSixCode());
                customerInfoList.get(0).setModifytime(String.valueOf(new Date().getTime()));
                customerInfoList.get(0).setMath_code(mathCode);
                customerInfoService.updateById(customerInfoList.get(0));
                try {
                    DayuSMSUtil.sendSms(CodeUtil.getJsonCode(mathCode), DayuSMSUtil.APPUSERLOGIN, customer_phone);
                } catch (Exception e) {
                    e.printStackTrace();
                    return new ModelRes(ModelRes.Status.ERROR, "验证码发送异常!");
                }
                return new ModelRes();
            }

        } else if (type == 2) {
            if (customerInfoList.size() > 0) {
                if (customerInfoList.get(0).getCreatetime() == null) {
                    String mathCode1 = String.valueOf(CodeUtil.getSixCode());
                    try {
                        DayuSMSUtil.sendSms(CodeUtil.getJsonCode(mathCode1), DayuSMSUtil.APPUSERREGISTER, customer_phone);
                        customerInfoList.get(0).setModifytime(String.valueOf(new Date().getTime()));
                        customerInfoList.get(0).setMath_code(mathCode1);
                        customerInfoService.updateById(customerInfoList.get(0));
                        return new ModelRes(200, "验证码发送成功!");
                    } catch (Exception e) {
                        e.printStackTrace();
                        return new ModelRes(ModelRes.Status.ERROR, "验证码发送异常!");
                    }
                } else {
                    return new ModelRes(ModelRes.Status.ERROR, "您已注册,请登录！");
                }

            } else {
                String mathCode = String.valueOf(CodeUtil.getSixCode());
                CustomerInfo customerInfo = new CustomerInfo();
                customerInfo.setCustomer_phone(customer_phone);
                try {
                    DayuSMSUtil.sendSms(CodeUtil.getJsonCode(mathCode), DayuSMSUtil.APPUSERREGISTER, customer_phone);
                    customerInfo.setModifytime(String.valueOf(new Date().getTime()));
                    customerInfo.setMath_code(mathCode);
                    customerInfoService.insert(customerInfo);
                    return new ModelRes(200, "验证码发送成功!");
                } catch (Exception e) {
                    e.printStackTrace();
                    return new ModelRes(ModelRes.Status.ERROR, "验证码发送异常!");
                }
            }


        }
        return new ModelRes(500, "类型异常!");

    }


    /**
     * 客户登录或注册
     *
     * @param customer_phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "customerLoginOrInsert", produces = "text/html;charset=utf-8")
    public Object customerLogin(String customer_phone, String math_code, int type, int terminalType) {
        Map map = new HashMap();
        map.put("customer_phone", customer_phone);
        List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
        if (type == 1) {
            if (customerInfos.size() == 0) {
                return new ModelRes(ModelRes.Status.ERROR, "没有此账户，登录失败!");
            } else {
                if (!customerInfos.get(0).getMath_code().equals(math_code)) {
                    return new ModelRes(ModelRes.Status.ERROR, "验证码错误，登录失败!");
                }
               /*if(new Date().getTime()-Long.parseLong(customerInfos.get(0).getModifytime())>300000){
                   return new ModelRes(500,"你的验证码已超时，请重新获取!");
               }*/
                customerInfos.get(0).setLogin_time(System.currentTimeMillis()+"");
                List<Integer> address_id = customerAddressService.getAddress_id(customerInfos.get(0).getCustomer_id());
                if (address_id != null || address_id.size() >= 1) {
                    for (Integer address : address_id) {
                        List<Insurance> insuranceByAddress_id = customerInsuranceService.getInsuranceByAdd(address);
                        if (insuranceByAddress_id != null || insuranceByAddress_id.size() >= 1) {
                            customerInfos.get(0).setIs_vip(2);
                        }
                    }
                }
                customerInfoService.updateById(customerInfos.get(0));

                statistics.setUsrName(customerInfos.get(0).getCustomer_id().toString());
                statistics.setAlias(customerInfos.get(0).getCustomer_name());
                statistics.setMobilePhoneNum(customerInfos.get(0).getCustomer_phone());
                statistics.setOpenId("-1");
                if (terminalType == 1) {
                    statistics.setTag("客户_Android");
                } else if (terminalType == 2) {
                    statistics.setTag("客户_Ios");
                } else {
                    statistics.setTag("客户_未知终端");
                }


                Thread thread = new Thread(t);
                thread.start();
                return new ModelRes(ModelRes.Status.SUCCESS, "登录成功", customerInfos.get(0));
            }
        } else if (type == 2) {
            if (customerInfos.size() > 0 && customerInfos.get(0).getMath_code().equals(math_code)) {
           /* if(new Date().getTime()-Long.parseLong(customerInfos.get(0).getModifytime())>300000){
                return new ModelRes(500,"你的验证码已超时，请重新获取!");
            }*/
                customerInfos.get(0).setIs_freeze(1);
                customerInfos.get(0).setIs_vip(1);
                customerInfos.get(0).setCustomer_phone(customer_phone);
                customerInfos.get(0).setLogin_time(String.valueOf(new Date().getTime()));
                customerInfos.get(0).setCreatetime(String.valueOf(new Date().getTime()));
                customerInfos.get(0).setModifytime(String.valueOf(new Date().getTime()));

                boolean isSuccess = customerInfoService.updateById(customerInfos.get(0));
                if (isSuccess) {
                    return new ModelRes(ModelRes.Status.SUCCESS, "注册成功", customerInfos.get(0));
                } else {
                    return new ModelRes(ModelRes.Status.ERROR, "注册失败!");
                }
            } else if (customerInfos.size() == 0) {
                return new ModelRes(ModelRes.Status.ERROR, "您尚未获取验证码,请重试!");
            } else {
                return new ModelRes(ModelRes.Status.ERROR, "验证码错误!");
            }

        }

        return new ModelRes(ModelRes.Status.ERROR, "type错误！");
    }

    @Override
    public void run() {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://www.innoobox.com/datacenter/putData");
        Map<String, String> params = new HashMap<String, String>();
        params.put("alias", statistics.getAlias());
        params.put("mobilePhoneNum", statistics.getMobilePhoneNum());
        params.put("opTag", statistics.getOpTag().toString());
        params.put("openId", statistics.getOpenId());
        params.put("tag", statistics.getTag());
        params.put("transCode", statistics.getTransCode());
        params.put("usrName", statistics.getUsrName());

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            nvps.add(new BasicNameValuePair(key, (String) params.get(key)));
        }

        System.out.println(nvps.toString());
        CloseableHttpResponse responsee = null;
        String content = "";
        try {
            //提交的参数
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
            //将参数给post方法
            httpPost.setEntity(uefEntity);

            System.out.println(uefEntity.toString());
            //执行post方法
            responsee = httpclient.execute(httpPost);
            if (responsee.getStatusLine().getStatusCode() == 200) {
                content = EntityUtils.toString(responsee.getEntity(), "utf-8");
                System.out.println(content);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询客户信息
     *
     * @param customer_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectCustomerInfo", produces = "text/html;charset=utf-8")
    public Object selectCustomerInfo(int customer_id) {
        CustomerInfo customerInfo = customerInfoService.selectById(Long.valueOf(customer_id));
        customerInfo.setLogin_time(String.valueOf(new Date().getTime()));
        List<Integer> address_id = customerAddressService.getAddress_id(customer_id);
        if (address_id != null || address_id.size() >= 1) {
            for (Integer address : address_id) {
                List<Insurance> insuranceByAddress_id = customerInsuranceService.getInsuranceByAdd(address);
                if (insuranceByAddress_id == null || insuranceByAddress_id.size() == 0) {

                } else {
                    customerInfo.setIs_vip(2);
                }
            }
        }
        customerInfoService.updateById(customerInfo);
        if (customerInfo != null) {
            CustomerInfo customerInfo1 = customerInfoService.selectById(Long.valueOf(customer_id));
            return new ModelRes(customerInfo1);
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "查询失败!");
        }
    }

    //更改绑定用户手机号
    @ResponseBody
    @RequestMapping(value = "customer_phone", produces = "text/html;charset=utf-8")
    public Object sendCode(String customer_phone, int type) {
        Map map = new HashMap();
        map.put("customer_phone", customer_phone);
        List<CustomerInfo> customerInfoList = customerInfoService.selectByMap(map);
        if (customerInfoList.size() == 0) {
            return new ModelRes(500, "该手机号不存在!");
        } else {
            CustomerInfo customer = customerInfoList.get(0);
            try {
                String code = CodeUtil.getSixCode().toString();
                SendSmsResponse sendSmsResponse = DayuSMSUtil.sendSms(CodeUtil.getJsonCode(code), DayuSMSUtil.CHANGINGBANGDING, customer_phone);
                System.out.println(sendSmsResponse);
                customer.setModifytime(String.valueOf(new Date().getTime()));
                customer.setMath_code(code);
                customerInfoService.updateById(customer);
            } catch (Exception e) {

                e.printStackTrace();
                return new ModelRes(500, "验证码发送失败!");
            }
            return new ModelRes(200, "验证码发送成功!");
        }


    }

    /**
     * 修改客户信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCustomerInfo", produces = "text/html;charset=utf-8")
    public Object updateCustomerInfo(String customerBean, int type, CustomerInfo customerInfo) {
        CustomerInfo customerInfo1 = customerInfoService.selectById(customerInfo.getCustomer_id());
        //用户名
        if (type == 1) {
            customerInfo1.setCustomer_name(customerBean);
        }
        //手机号码
        if (type == 2) {
            Map map = new HashMap();
            map.put("customer_phone", customerBean);
            List<CustomerInfo> customerInfos = customerInfoService.selectByMap(map);
            if (customerInfos.size() > 0) {
                if (!customerInfos.get(0).getMath_code().equals(customerInfo.getMath_code())) {
                    return new ModelRes(ModelRes.Status.ERROR, "验证码错误，修改失败!");
                }
                if (new Date().getTime() - Long.parseLong(customerInfos.get(0).getModifytime()) > 300000) {
                    return new ModelRes(500, "你的验证码已超时，请重新获取!");
                }
                if (customerInfos.get(0).getCustomer_id() != customerInfo.getCustomer_id() || customerInfos.size() > 1) {
                    return new ModelRes(ModelRes.Status.ERROR, "该手机号已存在，修改失败!");
                } else {
                    customerInfo1.setCustomer_phone(customerBean);

                }
            } else {
                customerInfo1.setCustomer_phone(customerBean);
            }

            try {
                DayuSMSUtil.sendSms(null, DayuSMSUtil.CHANGEPHONE, customerBean);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        customerInfo1.setModifytime(String.valueOf(new Date().getTime()));
        List<Integer> address_id = customerAddressService.getAddress_id(customerInfo1.getCustomer_id());
        if (address_id != null || address_id.size() >= 1) {
            for (Integer address : address_id) {
                List<Insurance> insuranceByAddress_id = customerInsuranceService.getInsuranceByAdd(address);
                if (insuranceByAddress_id == null || insuranceByAddress_id.size() == 0) {

                } else {
                    customerInfo.setIs_vip(2);
                }
            }
        }
        boolean isSuccess = customerInfoService.updateById(customerInfo1);
        if (isSuccess) {
            return new ModelRes(customerInfo1);
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "修改失败!");
        }
    }

    /**
     * 查询客户的地址列表
     *
     * @param customer_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectCustomerAddress", produces = "text/html;charset=utf-8")
    public Object selectCustomerAddress(int customer_id) {
        Map map = new HashMap();
        map.put("customer_id", customer_id);
        map.put("isDel", 1);
        List<CustomerAddress> customerAddressList = customerAddressService.selectByMap(map);
        for (CustomerAddress address : customerAddressList
                ) {
            List<Insurance> insuranceByAdd = customerInsuranceService.getInsuranceByAdd(address.getAddress_id());
            if (insuranceByAdd != null && insuranceByAdd.size() > 0) {
                address.setIsInsurance(1);
                break;
            } else {
                address.setIsInsurance(0);
            }


        }
        System.out.println(customerAddressList.toString());
        return new ModelRes(customerAddressList);
    }

    //查询维保
    @ResponseBody
    @RequestMapping(value = "getInsuranceByAddress_id", produces = "text/html;charset=utf-8")
    public Object getInsuranceByAddress_id(Integer address_id) {
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        List list3 = new ArrayList();
        List list4 = new ArrayList();
        List li = new ArrayList();
        Map map1 = new HashMap();
        Map map2 = new HashMap<String, Object>();
        Map map3 = new HashMap();
        Map map4 = new HashMap();
        ArrayEntity array = new ArrayEntity();
        BaseVO vo = null;
        List<Insurance> baseInsurance = customerInsuranceService.getInsuranceByAddress_id(address_id);
        if (baseInsurance == null || baseInsurance.size() < 1) {
            return new ModelRes(ModelRes.Status.ERROR, "未找到维保信息！");
        }
        for (Insurance ins : baseInsurance) {


            if (ins.getType().equals("中央空调")) {
                vo = new BaseVO();
                vo.setName(ins.getName());
                vo.setStatus(ins.getStatus());
                list1.add(vo);
                map1.put("type", ins.getType());
                map1.put("array", list1);
            } else if (ins.getType().equals("地暖")) {
                vo = new BaseVO();
                vo.setName(ins.getName());
                vo.setStatus(ins.getStatus());
                list2.add(vo);
                map2.put("type", ins.getType());
                map2.put("array", list2);
            } else if (ins.getType().equals("新风")) {
                vo = new BaseVO();
                vo.setName(ins.getName());
                vo.setStatus(ins.getStatus());
                list3.add(vo);
                map3.put("type", ins.getType());
                map3.put("array", list3);
            } else if (ins.getType().equals("水处理")) {
                vo = new BaseVO();
                vo.setName(ins.getName());
                vo.setStatus(ins.getStatus());
                list4.add(vo);
                map4.put("type", ins.getType());
                map4.put("array", list4);
            }

        }
        if (list1.size() >= 1) {
            li.add(map1);
        }
        if (list2.size() >= 1) {
            li.add(map2);
        }
        if (list3.size() >= 1) {
            li.add(map3);
        }
        if (list4.size() >= 1) {
            li.add(map4);
        }


        return new ModelRes(li);

    }

    //根据客户id查询维保
    @ResponseBody
    @RequestMapping(value = "getInsuranceByCustomer_id", produces = "text/html;charset=utf-8")
    public Object getInsuranceByCustom_id(int customer_id) {
        List<Integer> address = customerAddressService.getAddress_id(customer_id);

        if (address == null || address.size() < 1) {
            return new ModelRes(ModelRes.Status.ERROR, "该客户未添加地址！");
        }

        List list1 = new ArrayList();
        List list2 = new ArrayList();
        List list3 = new ArrayList();
        List list4 = new ArrayList();
        List li = new ArrayList();
        Map map1 = new HashMap();
        Map map2 = new HashMap();
        Map map3 = new HashMap();
        Map map4 = new HashMap();
        ArrayEntity array = new ArrayEntity();

        if (address != null && address.size() >= 1) {
            BaseVO vo1 = new BaseVO();
            vo1.setName("水系统");
            vo1.setStatus(0);
            BaseVO vo2 = new BaseVO();
            vo2.setName("vrv多联机");
            vo2.setStatus(0);
            BaseVO vo3 = new BaseVO();
            vo3.setName("水采暖");
            vo3.setStatus(0);
            BaseVO vo4 = new BaseVO();
            vo4.setName("电采暖");
            vo4.setStatus(0);
            BaseVO vo5 = new BaseVO();
            vo5.setName("无净化新风");
            vo5.setStatus(0);
            BaseVO vo6 = new BaseVO();
            vo6.setName("净化新风");
            vo6.setStatus(0);
            BaseVO vo7 = new BaseVO();
            vo7.setName("软水");
            vo7.setStatus(0);
            BaseVO vo8 = new BaseVO();
            vo8.setName("净水");
            vo8.setStatus(0);
            BaseVO vo9 = new BaseVO();
            vo9.setName("直饮水");
            vo9.setStatus(0);

            for (int j = 0; j < address.size(); j++) {
                int address_id = address.get(j);

                List<Insurance> baseInsurance = customerInsuranceService.getInsuranceByAddress_id(address_id);

                if (baseInsurance == null || baseInsurance.size() < 1) {
                    return new ModelRes(ModelRes.Status.ERROR, "地址未绑定保险！");
                }
                for (Insurance ins : baseInsurance) {


                    if (ins.getType().equals("中央空调")) {
                        if (ins.getName().equals("水系统") && ins.getStatus() == 1) {

                            vo1.setStatus(1);
                        }
                        if (ins.getName().equals("vrv多联机") && ins.getStatus() == 1) {

                            vo2.setStatus(1);
                        }

                    } else if (ins.getType().equals("地暖")) {
                        if (ins.getName().equals("水采暖") && ins.getStatus() == 1) {

                            vo3.setStatus(1);
                        }
                        if (ins.getName().equals("电采暖") && ins.getStatus() == 1) {

                            vo4.setStatus(1);
                        }

                    } else if (ins.getType().equals("新风")) {
                        if (ins.getName().equals("无净化新风") && ins.getStatus() == 1) {

                            vo5.setStatus(1);
                        }
                        if (ins.getName().equals("净化新风") && ins.getStatus() == 1) {

                            vo6.setStatus(1);
                        }

                    } else if (ins.getType().equals("水处理")) {
                        if (ins.getName().equals("净水") && ins.getStatus() == 1) {

                            vo7.setStatus(1);
                        }
                        if (ins.getName().equals("软水") && ins.getStatus() == 1) {

                            vo8.setStatus(1);
                        }
                        if (ins.getName().equals("直饮水") && ins.getStatus() == 1) {
                            vo9.setStatus(1);
                        }


                    }

                }


            }
            list1.add(vo1);
            list1.add(vo2);
            map1.put("type", "中央空调");
            map1.put("array", list1);

            list2.add(vo3);
            list2.add(vo4);
            map2.put("type", "地暖");
            map2.put("array", list2);

            list3.add(vo5);
            list3.add(vo6);
            map3.put("type", "新风");
            map3.put("array", list3);

            list4.add(vo7);
            list4.add(vo8);
            list4.add(vo9);
            map4.put("type", "水处理");
            map4.put("array", list4);
            if (list1.size() >= 1) {
                li.add(map1);
            }
            if (list2.size() >= 1) {
                li.add(map2);
            }
            if (list3.size() >= 1) {
                li.add(map3);
            }
            if (list4.size() >= 1) {
                li.add(map4);
            }


        }

        return new ModelRes(li);

    }


    /**
     * 添加地址
     *
     * @param customerAddress
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertCustomerAddress", produces = "text/html;charset=utf-8")
    public Object insertCustomerAddress(CustomerAddress customerAddress) {
        customerAddress.setIsDel(1);
        String times = String.valueOf(new Date().getTime());
        customerAddress.setCreatetime(times);
        customerAddress.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = customerAddressService.insert(customerAddress);

        Insurance insurance1 = new Insurance("水系统", 0, times, "中央空调");
        Insurance insurance2 = new Insurance("vrv多联机", 0, times, "中央空调");
        Insurance insurance3 = new Insurance("水采暖", 0, times, "地暖");
        Insurance insurance4 = new Insurance("电采暖", 0, times, "地暖");
        Insurance insurance5 = new Insurance("无净化新风", 0, times, "新风");
        Insurance insurance6 = new Insurance("净化新风", 0, times, "新风");
        Insurance insurance7 = new Insurance("净水", 0, times, "水处理");
        Insurance insurance8 = new Insurance("软水", 0, times, "水处理");
        Insurance insurance9 = new Insurance("直饮水", 0, times, "水处理");


        insurance1.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance2.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance3.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance4.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance5.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance6.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance7.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance8.setAddress_id(customerAddressService.getAddress_idByTime(times));
        insurance9.setAddress_id(customerAddressService.getAddress_idByTime(times));

        customerInsuranceService.insert(insurance1);
        customerInsuranceService.insert(insurance2);
        customerInsuranceService.insert(insurance3);
        customerInsuranceService.insert(insurance4);
        customerInsuranceService.insert(insurance5);
        customerInsuranceService.insert(insurance6);
        customerInsuranceService.insert(insurance7);
        customerInsuranceService.insert(insurance8);
        customerInsuranceService.insert(insurance9);

        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "添加失败!");
        }
    }

    /**
     * 修改客户地址
     *
     * @param customerAddress
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCustomerAddress", produces = "text/html;charset=utf-8")
    public Object updateCustomerAddress(CustomerAddress customerAddress) {
        CustomerAddress customerAddress1 = customerAddressService.selectById(customerAddress.getAddress_id());
        if (customerAddress.getAddress() != null) {
            customerAddress1.setAddress(customerAddress.getAddress());
        }
        if (customerAddress.getHouse_number() != null) {
            customerAddress1.setHouse_number(customerAddress.getHouse_number());
        }
        if (customerAddress.getPoi_lat() != null) {
            customerAddress1.setPoi_lat(customerAddress.getPoi_lat());
        }
        if (customerAddress.getPoi_lng() != null) {
            customerAddress1.setPoi_lng(customerAddress.getPoi_lng());
        }
        customerAddress1.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = customerAddressService.updateById(customerAddress1);
        if (isSuccess) {
            return new ModelRes(customerAddress1);
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "修改失败!");
        }
    }

    /**
     * 删除房产地址
     *
     * @param address_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteCustomerAddress", produces = "text/html;charset=utf-8")
    public Object deleteCustomerAddress(int address_id) {

        Map map = new HashMap();
        map.put("house_id", address_id);

        CustomerAddress customerAddress = customerAddressService.selectById(address_id);
        customerAddress.setIsDel(2);
        boolean isSuccess = customerAddressService.updateById(customerAddress);
        if (isSuccess) {
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "删除失败!");
        }
    }


//    /**
//     * 添加修改意见
//     * @param customer_id
//     * @param back_meark
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "updateCustomerBack",produces = "text/html;charset=utf-8")
//    public Object updateCustomerBack(int customer_id,String back_meark){
//        if(StringUtil.isBlank(back_meark)){
//            return new ModelRes(ModelRes.Status.ERROR,"意见为空，添加失败!");
//        }
//        CustomerInfo customerInfo = customerInfoService.selectById(Long.valueOf(customer_id));
//        customerInfo.setBack_meark(back_meark);
//        boolean isSuccess = customerInfoService.updateById(customerInfo);
//        if (isSuccess){
//            return new ModelRes(customerInfo);
//        }else {
//            return new ModelRes(ModelRes.Status.ERROR,"添加意见失败!");
//        }
//    }

    /**
     * 查询优惠券列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectCouponList", produces = "text/html;charset=utf-8")
    public Object selectCouponList(int customer_id) {
        Map map = new HashMap();
        // CustomerInfo customerInfo = customerInfoService.selectById(customer_id);
        map.put("customer_id", customer_id);
        map.put("is_out", 1);
        List<CouponView> couponViews = customerCouponService.selectCouponViewByCustomerId(map);
        return new ModelRes(couponViews);
    }

    /**
     * 客户添加修改意见
     *
     * @param customerIdea
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertCustomerIdea", produces = "text/html;charset=utf-8")
    public Object insertCustomerIdea(CustomerIdea customerIdea) {
        customerIdea.setCreatetime(System.currentTimeMillis() + "");
        boolean isSuccess = customerIdeaService.insert(customerIdea);
        if (isSuccess) {
            CustomerInfo customerInfo = customerInfoService.selectById(customerIdea.getCustomer_id());
            try {
                DayuSMSUtil.sendSms(null, DayuSMSUtil.APPSUGGESSTION, customerInfo.getCustomer_phone());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new ModelRes();
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "添加意见失败!");
        }
    }


    private CouponService couponService;

    @ResponseBody
    @RequestMapping(value = "linkCoupon", produces = "text/html;charset=utf-8")
    public Object linkCoupon(String phone) {

        if(StringUtil.isBlank(phone)){
            return new ModelRes(ModelRes.Status.ERROR, "请输入手机号!");
        }
        Map<String, String> map = new HashMap<>();
        map.put("customer_phone", phone);
        List<CustomerInfo> list = customerCouponService.getCouponAct(map);
        if (null == list || list.size() == 0) {
            //查询该电话的客户
            CustomerInfo newInfo = customerInfoService.selectCustomerByPhone(map);
            if (newInfo == null) {
                newInfo = new CustomerInfo();
                newInfo.setCustomer_phone(phone);
                newInfo.setCustomer_name(phone);
                newInfo.setIs_vip(1);
                newInfo.setIs_freeze(1);
                customerInfoService.insert(newInfo);
            }

            //查询优惠券
            Coupon coupon = new Coupon();
            coupon.setCoupon_status("1");
            coupon = couponService.selectOne(coupon);


            CustomerCoupon customerCoupon = new CustomerCoupon();
            customerCoupon.setCustomer_id(newInfo.getCustomer_id());
            customerCoupon.setIs_out(1);
            customerCoupon.setIs_use(1);
            customerCoupon.setCoupon_id(coupon.getId());
            customerCoupon.setCreatetime(System.currentTimeMillis() + "");
            customerCouponService.insert(customerCoupon);

            return new ModelRes(ModelRes.Status.SUCCESS, "领取成功!");
        } else {
            return new ModelRes(ModelRes.Status.ERROR, "您已领取过该优惠券!");
        }
    }


}
